package com.jeeplus.test.jlnu.reviewApprove.service.impl;

import cn.hutool.core.util.IdUtil;
import com.jeeplus.sys.domain.Office;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.test.jlnu.reviewApprove.domain.JlnuReview;
import com.jeeplus.test.jlnu.reviewApprove.domain.JlnuReviewApprove;
import com.jeeplus.test.jlnu.reviewApprove.domain.dto.ReviewDTO;
import com.jeeplus.test.jlnu.reviewApprove.mapper.JlnuReviewApproveMapper;
import com.jeeplus.test.jlnu.reviewApprove.mapper.JlnuReviewMapper;
import com.jeeplus.test.jlnu.reviewApprove.service.JlnuReviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 审核表(JlnuReview)表服务实现类
 *
 * @author ChiHongYu
 * @since 2025-02-24 09:11:46
 */
@Service("jlnuReview")
public class JlnuReviewServiceImpl implements JlnuReviewService {
    @Resource
    private JlnuReviewMapper jlnuReviewMapper;
    @Resource
    private JlnuReviewApproveMapper reviewApproveMapper;
    @Resource
    private OfficeService officeService;
    @Resource
    private UserService userService;


    @Override
    public Integer createReview(ReviewDTO reviewDTO) {
        UserDTO user = userService.get(reviewDTO.getSysUserId());
        Office office = officeService.get(user.getOfficeDTO().getId());
        if (office != null && !"0".equals(office.getParentId())) {
            String approveUserIds = userService.getUserIdsByOfficeId(office.getParentId());
            if (!"".equals(approveUserIds)) {
                Integer reviewState = 2;
                //创建主审批
                String UUID = IdUtil.simpleUUID();
                JlnuReview review = new JlnuReview();
                review.setReviewId(UUID);
                review.setReviewState(reviewState);
                review.setBelongingTable(reviewDTO.getBelongingTable());
                review.setReviewContent(reviewDTO.getReviewContent());
                review.setReviewContentId(reviewDTO.getReviewContentId());
                review.setReviewNotes(reviewDTO.getReviewNotes());
                review.setReviewRemark(reviewDTO.getReviewRemark());
                review.setCreator(reviewDTO.getSysUserId());

                //创建子审批
                JlnuReviewApprove reviewApprove = new JlnuReviewApprove();
                reviewApprove.setReviewId(UUID);
                reviewApprove.setApproveLevel(1);
                reviewApprove.setReviewUserId(reviewDTO.getSysUserId());
                reviewApprove.setApproveUserIds(approveUserIds);
                reviewApprove.setApproveState(reviewState);
                reviewApprove.setApproveRemark(reviewDTO.getReviewRemark());

            }
        }
        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param reviewId 主键
     * @return 实例对象
     */
    @Override
    public JlnuReview queryById(String reviewId) {
        return this.jlnuReviewMapper.queryById(reviewId);
    }


    @Override
    public List<JlnuReview> queryByLimit(Map<String, Object> param, int limit1, int limit2) {
        return this.jlnuReviewMapper.page(param, limit1, limit2);
    }

    @Override
    public int queryCount(Map<String, Object> param) {
        return this.jlnuReviewMapper.count(param);
    }

    /**
     * 新增数据
     *
     * @param jlnuReview 实例对象
     * @return 是否成功
     */
    @Override
    public boolean insert(JlnuReview jlnuReview) {
        return this.jlnuReviewMapper.insert(jlnuReview) > 0;
    }

    /**
     * 修改数据
     *
     * @param jlnuReview 实例对象
     * @return 是否成功
     */
    @Override
    public boolean update(JlnuReview jlnuReview) {
        return this.jlnuReviewMapper.update(jlnuReview) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param reviewId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String reviewId) {
        return this.jlnuReviewMapper.deleteById(reviewId) > 0;
    }

    /**
     * 通过主键修改使用状态
     *
     * @param reviewId 主键
     * @param useState 使用状态
     * @return 是否成功
     */
    @Override
    public boolean changeUseById(String reviewId, String useState) {
        return this.jlnuReviewMapper.changeUseById(reviewId, useState) > 0;
    }
}
